package com.cheer.tool;

import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.cheer.enter.VideoExportDTO;
import com.cheer.util.ToolUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 视频点赞数据导入工具
 * 读取视频点赞提供的基础数据
 * 生成SQL 收到导入到数据库
 */
public class VideoVoteDateImport {


    // SQL开始执行ID
    static int startId = 324;
    static String excelPath = "F:\\大学习\\活动点赞\\向上向善\\向上向善汇总\\向上向善汇总表（部分）.xlsx";
    static String photoPath = "F:\\大学习\\活动点赞\\我和2035有个约定\\我和2035有个约定（提供点赞素材）\\摄影\\";
    static String prefixPath = "vote5/photo/";
    static String activityId = "'5'";
    static String categoryId = "'14'";

    public static void main(String[] args) {

        // excel 数据
        ExcelReader reader = ExcelUtil.getReader(excelPath);
        reader.setHeaderAlias(VideoExportDTO.map);
        List<VideoExportDTO> videoExportDTOs = reader.read(0, 1, VideoExportDTO.class);
        // 图片文件数据
        Map<String, String> pngMap = getPNGMap(new File(photoPath));
        // 打印点赞数据
        printVoteSQL(videoExportDTOs, pngMap);

    }


    /**
     * 打印SQL
     * @param videoExportDTOs
     * @param pngMap
     */
    private static void printVoteSQL( List<VideoExportDTO> videoExportDTOs,
                               Map<String, String> pngMap ) {

        for (int i = 0; i < videoExportDTOs.size(); i++) {

            VideoExportDTO videoExportDTO = videoExportDTOs.get(i);

            // id 开始序号
            Integer id = i + startId;
            String personNumber = videoExportDTO.getIndex();
            String personName = videoExportDTO.getName();
            String remark = videoExportDTO.getUnit();

            // 找到对应的图片信息
            String intro = videoExportDTO.getLectureTitle();
            String story = videoExportDTO.getVideoLine();
            String picturePath = prefixPath + pngMap.get(personNumber);
            // String picturePath = prefixPath + "网文.png";

            // 获取人员编号
            personNumber = ToolUtils.getExVotePersonPersonNumber(personNumber);
            String insertExVotePerson = ToolUtils.getExVotePersonSql( id, activityId, categoryId, personNumber, personName, picturePath,remark );
            String insertExVotePersonExtra = ToolUtils.getExVotePersonExtraSql(id, intro, story);

            Console.log(insertExVotePerson);
            // Console.log(insertExVotePersonExtra);
        }
    }


    /**
     * 获取与图片名称对应的map
     * （图片名称以数字开头 1.XXX.png）
     * @return
     */
    private static Map<String, String> getPNGMap(File file) {

        Map<String, String> map = new HashMap<>();
        String[] list = file.list((dir, name) -> name.contains("png"));
        for (int i = 0; i < list.length; i++) {

            String fileName = list[i];
            Pattern pattern = Pattern.compile("^\\d*", Pattern.DOTALL);
            Matcher matcher = pattern.matcher(fileName);
            if (matcher.find()) {
                String result= matcher.group();
                map.put(result, fileName);
            }
        }
        return map;
    }

}

